// import md5 from 'md5';
class Request {
    constructor() {
        this.baseUrl = process.env.VUE_APP_REQUEST_URL;
        this.http = (type = 'GET', url = '', data = {}, header = {}) => {
            return new Promise((resolve, reject) => {
                let ts = Date.now();
                // let system = getApp().globalData.systemInfo.system;
                // let csrfToken = md5(`${ts}-${system}-${ts.toString(36)}`)
                uni.request({
                    method: type,
                    url: this.baseUrl + url,
                    data,
                    timeout: 10000,
                    dataType: 'json',
                    // withCredentials: true,
                    header: {
                        ts: ts,
                        // system,
                        // 'csrf-token': csrfToken,
                        ...header
                    },
                    success: (response) => {
                        if (response.statusCode == 200) {
                            uni.hideLoading();
                            resolve(response.data)
                        }
                        else {
                            uni.hideLoading();
                            reject(response)
                        }
                    },
                    fail(err) {
                        uni.hideLoading();
                        reject(err.errMsg || err)
                    }
                })/* .then((response) => {
                    let [error, res] = response;
                    if (res.statusCode == 200) {
                        resolve(res.data);
                        uni.hideLoading();
                    } else {
                        reject(res.data.error)
                        uni.hideLoading();
                    }
                }).catch(error => {
                    let [err] = error;
                    uni.hideLoading();
                    reject(err);
                }) */
            });
        }
    }
    get(url, data, header = {}) {
        return this.http('GET', url, data, header)
    }
    post(url, data, header = {}) {
        return this.http('POST', url, data, header)
    }

}
let instance = new Request();
export default instance;